/*
 * FileName: atomic.cc
 * Author: 李波(Edwin Lee)
 * Date: 2024-07-15 20:00:00
 * Version: 1.0
 * Description: 一个卖票程序测试atomic_int类型
 */
#include <chrono>
#include <iostream>
#include <atomic>
#include <thread>
#include <list>

volatile std::atomic_bool isReady(false);
volatile std::atomic_int tcount(0);

void task()
{
	while(!isReady)
	{
		// 线程出让当前cpu时间片，等待下一次调度
		std::this_thread::yield(); 		
	}

	for(int i = 0; i < 100; ++i)
	{
		++tcount;
	}
}

int main()
{
	std::list<std::thread> tlist;
	for(int i = 0; i < 10; ++i)
	{
		tlist.push_back(std::thread(task));
	}

	std::this_thread::sleep_for(std::chrono::seconds(3));
	isReady = true;

	for(std::thread& t : tlist)
	{
		t.join();
	}
	std::cout<<"count:"<<tcount<<std::endl;
	return 0;
}

